package com.car.advertising.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.car.advertising.CarApplication;
import com.car.advertising.R;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.db.UserInfoBiz;
import com.car.advertising.db.UserInfoEntiy;
import com.car.advertising.entity.TaskRecordEntity;
import com.car.advertising.util.AmountCalculationUtil;
import com.car.advertising.view.CustomDialog;
import com.car.advertising.view.PagerGalleryView;
import com.support.widget.JumpManager;
import com.support.widget.layout.BaseToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;
import com.support.widget.view.viewPager.ViewPagerCompat;

import java.util.ArrayList;
import java.util.List;

public class TaskDetailsActivity extends BaseToolBarActivity {

    //    private ViewPagerCompat detailsVp;
    private TextView detailsProfitTv;
    private TextView detailsProgressTv;
    private RelativeLayout detailsGuidanceRl;
    private RelativeLayout detailsGetRl;
    private TextView detailsNameTv;
    private TextView detailsContentTv;
    private TextView detailsGetTv;
    private String taskId = "";

    private List<ImageView> mImageViews = new ArrayList<>();
    private ImageView taskDetailsIv;
    private UserInfoEntiy userInfoEntiy;

    private CustomDialog customDialog;
    //    private PagerGalleryView pagerGalleryView;
    private LinearLayout homeSpotLl;

    @Override
    protected void initToolBar() {
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("任务详情");
        setToolBarTitleColor(R.color.black_bg);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_task_details);
//        detailsVp = (ViewPagerCompat) findViewById(R.id.task_details_vp);
        taskDetailsIv = (ImageView) findViewById(R.id.task_details_iv);
//        pagerGalleryView = (PagerGalleryView) findViewById(R.id.home_advertising_gallery_view);
        homeSpotLl = (LinearLayout) findViewById(R.id.home_spot_ll);

        detailsProfitTv = (TextView) findViewById(R.id.task_details_profit_tv);
        detailsProgressTv = (TextView) findViewById(R.id.task_details_progress_tv);
        detailsGuidanceRl = (RelativeLayout) findViewById(R.id.task_details_guidance_rl);
        detailsGetRl = (RelativeLayout) findViewById(R.id.task_details_get_rl);
        detailsNameTv = (TextView) findViewById(R.id.task_details_name_tv);
        detailsContentTv = (TextView) findViewById(R.id.task_details_content_tv);
        detailsGetTv = (TextView) findViewById(R.id.task_details_get_Tv);

//        detailsVp.setPageTransformer(true, new ZoomOutPageTransformer());
//        detailsVp.setAdapter(taskPagerAdapter);
//        detailsVp.setOnPageChangeListener(taskOnPageChangeListener);

        detailsGuidanceRl.setOnClickListener(this);
        detailsGetRl.setOnClickListener(this);
        detailsGetTv.setOnClickListener(this);

//        if () {
//            detailsGetTv.setEnabled(true);
//        } else {
//            detailsGetTv.setEnabled(false);
//        }

        userInfoEntiy = UserInfoBiz.getInstance(TaskDetailsActivity.this).loadData();

        Intent intent = getIntent();
        taskId = intent.getStringExtra("taskId");
        getDetailsData();
        setDetailsText(null);
    }

    private void setDetailsText(TaskRecordEntity taskRecord) {
        if (taskRecord != null) {
//            initData(taskRecord.getTaskImg());
            Glide.with(TaskDetailsActivity.this).load(taskRecord.getTaskImg()).into(taskDetailsIv);

//            initViewPager(taskRecord.getTaskImg(), pagerGalleryView, homeSpotLl);
            detailsProfitTv.setText(taskRecord.getUnitPrice());
            int subtractionAmout = AmountCalculationUtil.getSubtractionAmout(taskRecord.getPersonLimit(), taskRecord.getCurrentNum());
            detailsProgressTv.setText(subtractionAmout + "");
            detailsNameTv.setText(taskRecord.getTaskTitle());
            detailsContentTv.setText(taskRecord.getTaskContent());
        }
    }

    private void initViewPager(String adverts, PagerGalleryView mViewPager, LinearLayout jinpinSpotLl) {
        if (TextUtils.isEmpty(adverts)) {
            return;
        }
        List<String> advertsList = new ArrayList<>();
        advertsList.add(adverts);
        String[] imgurlList = new String[advertsList.size()];
        for (int i = 0; i < advertsList.size(); i++) {
            imgurlList[i] = advertsList.get(i);
        }
        // 轮播图
        mViewPager.startLl(this, imgurlList, 2000, jinpinSpotLl, imgurlList);
        mViewPager.setMyOnItemClickListener(new PagerGalleryView.MyOnItemClickListener() {

            @Override
            public void onItemClick(int curIndex) {

            }
        });
    }

    private void getDetailsData() {
        if (TextUtils.isEmpty(taskId)) {
            return;
        }
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("taskId", taskId));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_013, new OkHttpClientManager.ResultCallback<TaskRecordEntity>() {
            @Override
            public void onError(String request) {
                LogUtil.i(request);
            }

            @Override
            public void onResponse(TaskRecordEntity resp) {
                setDetailsText(resp);
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    private PagerAdapter taskPagerAdapter = new PagerAdapter() {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(mImageViews.get(position));
            return mImageViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView(mImageViews.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return mImageViews.size();
        }
    };

    private ViewPagerCompat.OnPageChangeListener taskOnPageChangeListener = new ViewPagerCompat.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            String string = (position + 1) == mImageViews.size() ? "" : "";
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {
        switch (v.getId()) {
            case R.id.task_details_guidance_rl:
                break;
            case R.id.task_details_get_rl:
                Bundle bundle = new Bundle();
                bundle.putString("taskId", taskId);
                JumpManager.gotoActivity(TaskDetailsActivity.this, ReceiveRecordActivity.class, false, bundle);
                break;
            case R.id.task_details_get_Tv:
                if (userInfoEntiy != null) {
                    switch (userInfoEntiy.getLicenseAuth()) {
                        case "0"://未认证
                            setCustomDialog("尚未认证车辆信息，是否前去认证？", "认证", "取消", false, "0");
                            break;
                        case "1"://审核中
                            setCustomDialog("您的车辆信息正在审核中", " 确认", "", true, "1");
                            break;
                        case "2"://审核通过
                            getTask();
                            break;
                        case "3"://审核不通过
                            setCustomDialog("您的车辆信息审核未通过，是否重新提交审核？", "是", "否", false, "3");
                            break;
                        default:
                            break;
                    }
                }
                break;
            default:
                break;
        }
    }

    private void setCustomDialog(String titleStr, String commit, String cancel, boolean hintCancel, String licenseType) {
        CustomDialog.Builder builder1 = new CustomDialog.Builder(TaskDetailsActivity.this);
        builder1.setTitle(titleStr);
        builder1.setPositiveButton(commit, new DialogClick('q', licenseType));
        if (hintCancel) {

        } else {
            builder1.setNegativeButton(cancel, new DialogClick('c', licenseType));
        }
        customDialog = builder1.create();
        customDialog.show();
    }

    class DialogClick implements DialogInterface.OnClickListener {

        char type;
        String licenseType;

        public DialogClick(char type, String licenseType) {
            this.type = type;
            this.licenseType = licenseType;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (type) {
                case 'q':
                    customDialog.dismiss();
                    if (!licenseType.equals("1")) {
                        JumpManager.gotoActivity(TaskDetailsActivity.this, CarOwnerMessageActivity.class, false);
                    }
                    break;
                case 'c':
                    customDialog.dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    private void initData(String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        ImageView imageView = new ImageView(getApplicationContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(TaskDetailsActivity.this).load(url).into(imageView);
        mImageViews.add(imageView);
    }

    private void getTask() {
        if (TextUtils.isEmpty(taskId)) {
            return;
        }
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("taskId", taskId));
        params.add(new OkHttpClientManager.Param("acToken", CarApplication.token));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_016, new OkHttpClientManager.ResultCallback<TaskRecordEntity>() {
            @Override
            public void onError(String request) {
                ToastUtil.show(TaskDetailsActivity.this, "领取失败", Toast.LENGTH_SHORT);
            }

            @Override
            public void onResponse(TaskRecordEntity resp) {
                detailsGetTv.setEnabled(false);
                ToastUtil.show(TaskDetailsActivity.this, "领取成功", Toast.LENGTH_SHORT);
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }
}
